﻿@model Xms.Web.Models.AttachmentsModel
@{
    Layout = null;
}
@{
    DialogModel dialogModel = ViewData["DialogModel"] as DialogModel;
}

<!-- 附件（Modal） -->
<div class="modal fade" id="attachmentModal" tabindex="-1" role="dialog"
     aria-labelledby="attachmentModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">
                    ×
                </button>
                <h4 class="modal-title" id="attachmentModalLabel">
                    <span class="glyphicon glyphicon-th"></span> 附件
                </h4>
            </div>
            <div class="modal-body">
                <ul id="myTab" class="nav nav-tabs">
                    <li>
                        <a href="#infoPane" data-toggle="tab">
                            添加
                        </a>
                    </li>
                    <li class="active"><a href="#listPane" data-toggle="tab">列表</a></li>
                </ul>
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane" id="infoPane">
                        <div class="container-fluid" style="margin-top:10px;margin-bottom:10px;">
                            <form class="form-horizontal" action="@("/"+app.OrganizationUniqueName)/file/Create" method="post" enctype="multipart/form-data">
                                <input type="hidden" name="entityid" value="@Model.EntityId" />
                                <input type="hidden" name="objectid" value="@Model.ObjectId" />
                                <div class="form-group col-sm-12">
                                    <label class="col-sm-2 control-label" for="description">附件</label>
                                    <div class="col-sm-10">
                                        <input type="file" id="lefile" name="attachments" />
                                        <input type="text" class="form-control input-large " id="viewCover" aria-describedby="basic-addon1" name="attachment" readonly disabled="disabled" />
                                        <span class="input-group-addon glyphicon glyphicon-remove-sign" id="input-file-clear" onclick="inputFileClear();"></span>
                                        <span class="input-group-addon glyphicon glyphicon-open" id="basic-addon1" onclick="$('input[id=lefile]').click();"></span>
                                    </div>
                                </div>
                                <div class="form-group col-sm-12">
                                    <label class="col-sm-2 control-label" for="description">说明</label>
                                    <div class="col-sm-10">
                                        <textarea rows="3" cols="20" class="form-control" name="description" id="description"></textarea>
                                    </div>
                                </div>
                                <script type="text/javascript">
                                    $('input[id=lefile]').change(function () {
                                        var str;
                                        str = $(this).val();
                                        var arr = str.split('\\');
                                        var my = arr[arr.length - 1];
                                        $('#viewCover').val(my);
                                    });
                                    function inputFileClear() {
                                        $('#lefile').val('');
                                        $('#viewCover').val('');
                                    }
                                </script>
                                <div class="form-group col-sm-10 ">
                                    <div class="col-sm-offset-3">
                                        <button type="button" class="btn btn-primary" onclick="AttachmentModel.submit()"><span class="glyphicon glyphicon-saved"></span> @app.T["save"]</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                    <div class="tab-pane in active" id="listPane">
                        <div class="table-responsive" id="gridview">
                            <table class="table table-striped table-hover table-condensed" id="datatable" data-ajax="true" data-ajaxcontainer="gridview" data-ajaxcallback="AttachmentModel.rebind('')" data-sortby="@Model.SortBy.ToLower()" data-sortdirection="@Model.SortDirection" data-singlemode="@dialogModel.SingleMode" data-refresh="AttachmentModel.rebind('')">
                                <thead>
                                    <tr>
                                        <th width="2%"><input type="checkbox" name="checkall" /></th>
                                        <th data-name="name">
                                            @(Model.AttributeMetaDatas.Find(n => n.Name.IsCaseInsensitiveEqual("name")).LocalizedName)
                                        </th>
                                        <th data-name="cdnpath">
                                            @(Model.AttributeMetaDatas.Find(n => n.Name.IsCaseInsensitiveEqual("cdnpath")).LocalizedName)
                                        </th>
                                        <th data-name="createdon">
                                            @(Model.AttributeMetaDatas.Find(n => n.Name.IsCaseInsensitiveEqual("createdon")).LocalizedName)
                                        </th>
                                        <th data-name="createdby">
                                            @(Model.AttributeMetaDatas.Find(n => n.Name.IsCaseInsensitiveEqual("createdby")).LocalizedName)
                                        </th>
                                        <th></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var item in Model.Items)
                                    {
                                        <tr>
                                            <td><input type="checkbox" value="@item["attachmentid"]" name="recordid" /></td>
                                            <td>@item["name"]</td>
                                            <td>
                                                <a href="@("/"+app.OrganizationUniqueName)/file/Download?id=@item["attachmentid"]&sid=@app.CurrentUser.SessionId&preview=true" target="_blank">预览</a>
                                                <a href="@("/"+app.OrganizationUniqueName)/file/Download?id=@item["attachmentid"]&sid=@app.CurrentUser.SessionId" target="_blank">下载</a>
                                            </td>
                                            <td>@item["createdon"]</td>
                                            <td><a href="@("/"+app.OrganizationUniqueName)/entity/create?entityname=systemuser&recordid=@item["createdby"]" target="_blank">@item["createdbyname"]</a></td>
                                            <td>
                                                <a class="btn btn-warning btn-xs" href="javascript:Xms.Web.Del('@item["attachmentid"]','@("/"+app.OrganizationUniqueName)/file/delete?entityid=@Model.EntityId&objectid=@Model.ObjectId',false,AttachmentModel.rebind,null,'确定删除？')"><span class="glyphicon glyphicon-trash"></span> 删除</a>
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <td colspan="8" class="active">
                                            <button type="button" class="btn btn-xs btn-primary" data-action="@("/"+app.OrganizationUniqueName)/file/delete?entityid=@Model.EntityId&objectid=@Model.ObjectId" data-role="delete" data-tooltip="@app.T["delete_confirm"]"><span class="glyphicon glyphicon-trash"></span> @app.T["batch_delete"]</button>
                                        </td>
                                    </tr>
                                </tfoot>
                            </table>
                            <div class="panel-footer">
                                <div class="row">
                                    <div class="col-sm-4">
                                        @(app.T["pagination_label"].Replace("{page}", Model.Page.ToString()).Replace("{totalpages}", Model.TotalPages.ToString()).Replace("{totalitems}", Model.TotalItems.ToString()))
                                    </div>
                                    <div id="page-selection" class="col-sm-8" data-total="@Model.TotalPages" data-page="@Model.Page"></div>
                                </div>
                            </div>
                            <script src="/content/js/jquery.bootpag.min.js?v=@app.PlatformSettings.VersionNumber"></script>
                            <script src="/content/js/jquery.form.js?v=@app.PlatformSettings.VersionNumber"></script>
                            <script>
                                var AttachmentModel = {
                                    dialog: $('#attachmentModal'),
                                    dialogid: '#attachmentModal',
                                    pageUrl : '@app.Url',
                                    callback : @dialogModel.CallBack,
                                    inputid : '@dialogModel.InputId',
                                    entityid : '@(Model.EntityId.ToString())',
                                    objectid : '@(Model.ObjectId.ToString())',
                                    disabledForm: function () {
                                        AttachmentModel.dialog.find('button').prop('disabled', true);
                                    },
                                    resetForm: function () {
                                        AttachmentModel.dialog.find('button').prop('disabled', false);
                                    },
                                    ajaxgrid_reset: function () {
                                        AttachmentModel.pag_init();
                                        Xms.Web.DataTable($(AttachmentModel.dialogid + " #datatable"));
                                    },
                                    pag_init : function () {
                                        $(AttachmentModel.dialogid + ' #page-selection').bootpag({
                                            total: $(AttachmentModel.dialogid + ' #page-selection').attr('data-total')
                                            , maxVisible: 5 //最大显示分页个数
                                            , page: $(AttachmentModel.dialogid + ' #page-selection').attr('data-page')
                                            , leaps: false
                                            , prev: '&lsaquo;'
                                            , next: '&rsaquo;'
                                            , firstLastUse: true
                                            , first: '&laquo;'
                                            , last: '&raquo;'
                                        }).on("page", function (event, /* page number here */ num) {
                                            event.preventDefault();
                                            var url = $.setUrlParam(AttachmentModel.pageUrl, 'page', num);
                                            AttachmentModel.rebind(url);
                                            return false;
                                        });
                                    },
                                    submit: function () {
                                        AttachmentModel.disabledForm();
                                        AttachmentModel.dialog.find('form').submit();
                                    },
                                    dialog_return : function () {
                                        AttachmentModel.dialog.modal('hide');
                                        //Xms.Web.CloseDialog('#attachmentModal');
                                    },
                                    rebind: function (url) {
                                        if (typeof url == 'string') url = url || AttachmentModel.pageUrl;
                                        else url = AttachmentModel.pageUrl;
                                        $(AttachmentModel.dialogid + " #gridview").ajaxLoad(url, AttachmentModel.dialogid + " #gridview", function (response) {
                                            AttachmentModel.ajaxgrid_reset();
                                        });
                                        AttachmentModel.ajaxgrid_reset();
                                    }
                                };

                                $(function () {
                                    if(AttachmentModel.entityid == Xms.Utility.Guid.EmptyGuid.ToString() || AttachmentModel.objectid == Xms.Utility.Guid.EmptyGuid.ToString()){
                                        Xms.Web.Alert(false, LOC_NOTSPECIFIED_RECORD);
                                        AttachmentModel.dialog_return();
                                        return;
                                    }
                                    AttachmentModel.ajaxgrid_reset();

                                    AttachmentModel.dialog.find('form').ajaxForm({
                                        success:function (response) {
                                            if (response.IsSuccess) {
                                                Xms.Web.Alert(true, response.Content);
                                                //AttachmentModel.dialog_return();
                                                AttachmentModel.callback(response.Extra.filepath, AttachmentModel);
                                                AttachmentModel.rebind();
                                                AttachmentModel.dialog.find('form').resetForm();
                                                AttachmentModel.resetForm();
                                                return;
                                            }
                                            Xms.Web.Alert(false, response.Content);
                                            AttachmentModel.resetForm();
                                        },
                                        error:function(){
                                            AttachmentModel.dialog.find('form').resetForm();
                                            AttachmentModel.resetForm();
                                            setTimeout(function(){
                                                if($('.jq-toast-wrap').length>0){
                                                    $('.jq-toast-wrap').remove();
                                                }
                                            },2000);
                                            return;
                                        }
                                    });
                                    $(AttachmentModel.dialogid + " #datatable").ajaxTable();
                                    AttachmentModel.dialog.modal({
                                        backdrop:'static'
                                    })
                                    AttachmentModel.dialog.on('hidden.bs.modal', function () {
                                        Xms.Web.CloseDialog(AttachmentModel.dialogid);
                                    });
                                    //$('#myTab').find('li').click(function(){
                                    //    AttachmentModel.ajaxgrid_reset();
                                    //})
                                });
                            </script>
                        </div>
                    </div>
                </div>
            </div>
            @*<div class="modal-footer">
                    <button type="button" class="btn btn-default"
                            data-dismiss="modal">
                        <span class="glyphicon glyphicon-remove"></span>
                        @app.T["dialog_close"]
                    </button>
                    <button type="button" class="btn btn-primary" onclick="AttachmentModel.dialog_return()">
                        <span class="glyphicon glyphicon-ok"></span>
                        @app.T["dialog_ok"]
                    </button>
                </div>*@
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->